package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) throws Exception {
        // 获取数据库连接
        Connection conn = DBUtil.getConnection();

        // 创建Statement对象，用于执行SQL语句
        Statement statement = conn.createStatement();

        // 定义SQL查询语句，查询teacher表中的id、name、salary、title字段
        String sql = "SELECT id,name,salary,title FROM teacher";

        // 执行查询，获取ResultSet结果集
        ResultSet rs = statement.executeQuery(sql);

        // 遍历结果集
        while (rs.next()) {
            // 从结果集中获取各个字段的值
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double salary = rs.getDouble("salary");
            String title = rs.getString("title");

            // 输出查询结果
            System.out.println(id + "-" + name + "-" + salary + "-" + title);
        }

        // 关闭数据库连接
        conn.close();
    }

}
